import pandas as pd
import matplotlib.pyplot as plt

# 1.读数据
df = pd.read_csv ("去哪儿2020年五一旅游数据.csv", encoding = "utf8", sep=",")
# 2.数据清洗
df0 = df.drop(['Unnamed: 0','Unnamed: 0.1'],axis=1)                  # 删除不需要的数据
df0.columns = ['景点名称','星级','评分','介绍','经纬度','所在地区','儿童旅客量','销售量','门票']    # 表格列名重命名
df1 = df0.drop_duplicates(['景点名称']).reset_index()              # 删除景点名称 重复值

# 将地区细分为省、市、区
district = df1['所在地区'].tolist()
地区详情 = [ x.split("·") for x in district] 
省_list = [i[0] for i in 地区详情]
市_list = [i[1] for i in 地区详情]

for i in 地区详情:
    if len(i) >= 4:
        a = i
        del a[3]
    elif len(i) < 3:
        b = i
        b.append('NaN')
区_list = [i[2] for i in 地区详情]

# 将处理好的省、市、区数据添加进表格里
df1['省'],df1['市'],df1['区'] = [省_list,市_list,区_list]
df_new = df1



# 景点所属省份列表
省 = pd.unique(df_new['省'])
province_list =list(set(省))
# 景点所属市列表
市 = pd.unique(df_new['市'])
city_list =list(set(市))
# 景点所属区域列表
区 = pd.unique(df_new['区'])
district_list =list(set(区))
# 景点门票价格列表
price_list = ['免费','50元以下',
          '50-100元','100-200元','200-300元',
          '300-500元','700-1000元','1000以上']
# 景点星级列表
星级 = pd.unique(df_new['星级'])
star_list =list(set(星级))


# 根据price门票价格，star景点星级，province省份三个指标进行筛选景点 函数
def lookup(price,star,province):    
    if province in province_list and price in price_list and star in star_list:
            if price in price_list[0]:
                result = df_new[(df_new['门票']==0.0)&(df_new['星级']==star)&(df_new['省']==province)]
                result = result[['景点名称','星级','评分','介绍','所在地区','销售量','门票']] \
                .sort_values('销售量',ascending=False)
            elif price in price_list[1]:
                result = df_new[(df_new['门票']<=50.0)&(df_new['星级']==star)&(df_new['省']==province)]
                result = result[['景点名称','星级','评分','介绍','所在地区','销售量','门票']] \
                .sort_values('销售量',ascending=False)
            elif price in price_list[2]:
                result = df_new[(df_new['门票']>50.0)&(df_new['门票']<=100.0)&(df_new['星级']==star) \
                &(df_new['省']==province)]
                result = result[['景点名称','星级','评分','介绍','所在地区','销售量','门票']] \
                .sort_values('销售量',ascending=False)                
            elif price in price_list[3]:
                result = df_new[(df_new['门票']>100.0)&(df_new['门票']<=200.0)&(df_new['星级']==star) \
                &(df_new['省']==province)]
                result = result[['景点名称','星级','评分','介绍','所在地区','销售量','门票']] \
                .sort_values('销售量',ascending=False)                
            elif price in price_list[4]:
                result = df_new[(df_new['门票']>200.0)&(df_new['门票']<=300.0)&(df_new['星级']==star) \
                &(df_new['省']==province)]
                result = result[['景点名称','星级','评分','介绍','所在地区','销售量','门票']] \
                .sort_values('销售量',ascending=False) 
            elif price in price_list[5]:
                result = df_new[(df_new['门票']>300.0)&(df_new['门票']<=500.0)&(df_new['星级']==star) \
                &(df_new['省']==province)]
                result = result[['景点名称','星级','评分','介绍','所在地区','销售量','门票']] \
                .sort_values('销售量',ascending=False)
            elif price in price_list[6]:
                result = df_new[(df_new['门票']>500.0)&(df_new['门票']<=700.0)&(df_new['星级']==star) \
                &(df_new['省']==province)]
                result = result[['景点名称','星级','评分','介绍','所在地区','销售量','门票']] \
                .sort_values('销售量',ascending=False)
            elif price in price_list[7]:
                result = df_new[(df_new['门票']>700.0)&(df_new['门票']<=1000.0)&(df_new['星级']==star) \
                &(df_new['省']==province)]
                result = result[['景点名称','星级','评分','介绍','所在地区','销售量','门票']] \
                .sort_values('销售量',ascending=False)
            else:
                result = df_new[(df_new['门票']>1000.0)&(df_new['星级']==star)&(df_new['省']==province)]
                result = result[['景点名称','星级','评分','介绍','所在地区','销售量','门票']] \
                .sort_values('销售量',ascending=False)                                                                

    else:
        print('查询无效，请重新选择3个指标！') 

    return result


# 查看不同价位区间消费情况函数
i = '门票'
def payment(i):
    消费群体_list = ['50元以下','50-100元','100-200元',
    '200-300元','300-500元','500-700元','700元以上']

    choose=df_new[(df_new[i]<=50.0)]
    消费群体1 = choose['销售量'].sum()
    choose=df_new[(df_new[i]>50.0)&(df_new[i]<=100.0)]
    消费群体2 = choose['销售量'].sum()
    choose=df_new[(df_new[i]>100.0)&(df_new[i]<=200.0)]
    消费群体3 =choose['销售量'].sum()
    choose=df_new[(df_new[i]>200.0)&(df_new[i]<=300.0)]
    消费群体4 =choose['销售量'].sum()
    choose=df_new[(df_new[i]>300.0)&(df_new[i]<=500.0)]
    消费群体5 =choose['销售量'].sum()
    choose=df_new[(df_new[i]>500.0)&(df_new[i]<=700.0)]
    消费群体6 =choose['销售量'].sum()
    choose=df_new[(df_new[i]>700.0)]
    消费群体7 =choose['销售量'].sum()

    消费群体数量_list = [消费群体1,消费群体2,
    消费群体3,消费群体4,消费群体5,消费群体6,消费群体7]
    不同价位区间消费情况_dict = dict(zip(消费群体_list,消费群体数量_list))

    df_不同价位区间消费情况0 = pd.DataFrame(不同价位区间消费情况_dict,index=[0]).T.reset_index()
    df_不同价位区间消费情况0.columns = ['价格区间','消费人群总量']
    df_不同价位区间消费情况 = df_不同价位区间消费情况0.sort_values('消费人群总量',ascending=False)
    result = df_不同价位区间消费情况
    return result


# def select(province,city,district,price,star):
#     """
#     select函数是根据不同指标进行景点筛选的函数
#     其中province是省份，city是城市，district是区域，
#     price是景点门票价格，star是景点星级
#     """
#     if province in province_list and city in city_list \
#     and district in district_list and price in price_list \
#     and star in star_list:
#         result = df_new[(df_new["省"]==province)&(df_new["市"]==city) & \
#         (df_new["区"]==district)&(df_new["门票"]==price)&(df_new["星级"]==star)]

#     else:
#         print('查询无效，请重新选择五个指标！') 

#     return result